import SJGNode = require('./SJGNode');
import GCJ_CLASS_TYPE = require('./../GCJ_CLASS_TYPE');
import Serializer = require('../../kernel/base/Serializer')

export = SJGTG;
/**
 * 题纲对象
 * @author 
 *
 */
class SJGTG extends Serializer{
    public root: SJGNode=null;
    public version: string="0";
    public name: string="";

    public constructor(g?) {
        super(g);

        this.ct=GCJ_CLASS_TYPE.CLASS_TYPE_SJGTG;
    }
    // 检查数据
    public checkType():Boolean{
        return this.root.checkType();
    }
    public checkPage(): Boolean {
        return this.root.checkPage();
    }
    // 从json对象(注意不是json字符串)中解析
    public fromJson(jsonObj,versionId) {
        super.fromJson(jsonObj,versionId);

        if(jsonObj.hasOwnProperty("root")) {
            this.root=new SJGNode(this.getGlobalData());
            this.root.fromJson(jsonObj.root, versionId);
        }
        this.version=jsonObj.version;
        this.name=jsonObj.name;
    }
    // 转成json字符串
    public toJson():string{
        // 去掉父json字符串的头尾
        var superJson=super.toJson();
        superJson=superJson.substr(1,superJson.length-2);

        var ret='{';
        ret+=superJson;

        // version
        ret+=',';
        ret+='"version":';
        ret+=this.version;

        // name
        ret+=',';
        ret+='"name":'+'"';
        ret+=this.name+'"';

        // root
        if(this.root) {
            ret+=',';
            ret+='"root":';
            ret+=this.root.toJson();
        }

        ret+='}';
        return ret;
    }

    // 转成json字符串
    public toJson2():string{
        // 去掉父json字符串的头尾
        var superJson=super.toJson();
        superJson=superJson.substr(1,superJson.length-2);

        var ret='{';
        ret+=superJson;

        // version
        ret+=',';
        ret+='"version":';
        ret+=this.version;

        // name
        ret+=',';
        ret+='"name":'+'"';
        ret+=this.name+'"';

        // root
        if(this.root) {
            ret+=',';
            ret+='"root":';
            ret+=this.root.toJson2();
        }

        ret+='}';
        return ret;
    }

    // 遍历第一个node，深度遍历优先
    private current: SJGNode=null;
    public first():SJGNode{
        this.root.clearVisit();
        this.current=this.root;

        return this.next();
    }
    // 遍历下一个node，深度遍历优先
    public next(): SJGNode {
        if(this.current==null) return null;

        /*
         如果current被访问过，则退回父进行处理
         如果current未被访问过，
         如果current没有子，则current设置为访问，返回current
         如果current有子，则找出未访问的子；
         如果存在，则处理该子
         如果不存在，则current设置为访问，返回current
         */
        if(this.current.isVisit()){
            this.current=this.current.parent;
            return this.next();
        }else{
            var len=this.current.children.length;
            if(len==0) {
                this.current.setVisit();
                return this.current;
            }else{
                var uvChild: SJGNode=null;
                for(var i = 0; i < len; i++) {
                    var child: SJGNode=this.current.children[i];
                    if(!child.isVisit()) {
                        uvChild=child;
                        break;
                    }
                }
                if(uvChild) {
                    this.current=uvChild;
                    return this.next();
                }else{
                    this.current.setVisit();
                    return this.current;
                }
            }
        }

    }

    // 计算题纲树的特征值
    public calcEigenValue(): string {
        var eigen=[];
        var leafs: SJGNode[]=this.root.getLeafNodes();
        var process={};// 处理过的节点
        for(var i=0;i<leafs.length;i++) {
            var leaf: SJGNode=leafs[i];
            if(process.hasOwnProperty('"'+leaf.id+'"')) continue;

            if(!leaf.is_child_node_of_group_kind()) {
                // 普通节点，直接搜集
                // type:自身（不包含子）的表现形式1(0无/1文字/2图片/3图表/4表格)
                if(leaf.type==SJGNode.SJGNODE_TYPE_1) {
                    eigen.push(leaf.value.max);
                    process[leaf.id]=1; // 记录处理过的节点
                } else if(leaf.type==SJGNode.SJGNODE_TYPE_2) {
                    eigen.push(1);
                    process[leaf.id]=1; // 记录处理过的节点
                }
            } else {
                // 找到同一个数组下的同一级的所有叶节点，计算最大值
                var level = leaf.index();
                var grandSons = leaf.parent.parent.getGroupGrandSonOfLevel(level);
                var max=0;
                for(var k=0;k<grandSons.length; k++) {
                    var gs=grandSons[k];
                    if(gs.type==SJGNode.SJGNODE_TYPE_1) {
                        max=max>gs.value.max? max:gs.value.max;
                    } else if(gs.type==SJGNode.SJGNODE_TYPE_2) {
                        max=1;
                    }
                }
                eigen.push(max);

                // 记录处理过的节点
                for(var k=0;k<grandSons.length;k++) {
                    var gs=grandSons[k];
                    process[gs.id]=1;
                }
            }
        }
        return JSON.stringify(eigen);
    }

    // 更新数组节点的child_template
    public updateChildTemplate(){
        this.root.updateChildTemplate();
    }

    // 增加测试页
    public addTestData() {
        var str='{"version":"1","name":"333","root":{"id":"1","topic":"会议邀请函","type":1,"kind":0,"page_index":null,"value":null,"obj_id":null,"children":[{"id":"11","topic":"会议名称","type":1,"kind":3,"page_index":0,"value":{"ct":808,"type":20,"id":0,"value":"第五届国际互联网大会","min":0,"max":10},"obj_id":null,"children":[]},{"id":"12","topic":"会议图片","type":2,"kind":3,"page_index":1,"value":{"ct":805,"type":30,"id":0,"value":"http://itbour-generate.img-cn-hangzhou.aliyuncs.com/image/U10/2016/09/22/161121182_6SMbTjIIz6c1AJ0KEZos/0.jpg","pos":{"ct":2,"x":0,"y":0,"an":0,"w":100,"h":100}},"obj_id":null,"children":[]},{"id":"13","topic":"邀请嘉宾","type":1,"kind":1,"page_index":2,"value":{"ct":808,"type":20,"id":0,"value":"邀请嘉宾","min":0,"max":10},"obj_id":null,"children":[{"id":"131","topic":"嘉宾1","type":1,"kind":2,"page_index":2,"value":{"ct":808,"type":20,"id":0,"value":"嘉宾1","min":0,"max":10},"obj_id":null,"children":[{"id":"1311","topic":"名称1","type":1,"kind":3,"page_index":2,"value":{"ct":808,"type":20,"id":0,"value":"王先生","min":0,"max":10},"obj_id":null,"children":[]},{"id":"1312","topic":"头像1","type":2,"kind":3,"page_index":2,"value":{"ct":805,"type":30,"id":0,"value":"http://itbour-generate.img-cn-hangzhou.aliyuncs.com/image/U10/2016/09/22/161121182_6SMbTjIIz6c1AJ0KEZos/0.jpg","pos":{"ct":2,"x":0,"y":0,"an":0,"w":100,"h":100}},"obj_id":null,"children":[]}]},{"id":"132","topic":"嘉宾2","type":1,"kind":2,"page_index":2,"value":{"ct":808,"type":20,"id":0,"value":"嘉宾2","min":0,"max":10},"obj_id":null,"children":[{"id":"1321","topic":"名称2","type":1,"kind":3,"page_index":2,"value":{"ct":808,"type":20,"id":0,"value":"王先生","min":0,"max":10},"obj_id":null,"children":[]},{"id":"1322","topic":"头像2","type":2,"kind":3,"page_index":2,"value":{"ct":805,"type":30,"id":0,"value":"http://itbour-generate.img-cn-hangzhou.aliyuncs.com/image/U10/2016/09/22/161121182_6SMbTjIIz6c1AJ0KEZos/0.jpg","pos":{"ct":2,"x":0,"y":0,"an":0,"w":100,"h":100}},"obj_id":null,"children":[]}]}]}]}}';
        this.fromJson(JSON.parse(str),0);

//        var n: SJGNode=this.first();
//        while(n) {
//            console.log(n.id+' '+n.topic+' '+n.toJson()+'\n');
//
//            n=this.next();
//        }

//        alert(this.root.toJson());
        return;
//        // kind:自身加子的整体的类型(0根节点/1数组/2结构体/3叶节点)
//        // type:自身（不包含子）的表现形式1(0无/1文字/2图片/3图表/4表格)
//        var root: SJGNode=new SJGNode();
//        root.kind=SJGNode.SJGNODE_KIND_0;
//        root.type=SJGNode.SJGNODE_TYPE_1;
//        root.id=1;
//        root.topic="会议邀请函"
//        this.root=root;
//
//        // 子节点：会议主题
//        var sub1: SJGNode=new SJGNode();
//        sub1.kind=SJGNode.SJGNODE_KIND_3;
//        sub1.type=SJGNode.SJGNODE_TYPE_1;
//        sub1.id=11;
//        sub1.topic="会议主题"
//        sub1.page_index=0;
//        var t: SJGReplaceItemText=new SJGReplaceItemText();
//        t.value="第五届国际互联网大会"
//        t.min=0;
//        t.max=t.value.length;
//        sub1.value=t;
//        root.children.push(sub1);
//
//        // 子节点：活动图片
//        var sub1: SJGNode=new SJGNode();
//        sub1.kind=SJGNode.SJGNODE_KIND_3;
//        sub1.type=SJGNode.SJGNODE_TYPE_2;
//        sub1.id=11;
//        sub1.topic="活动图片"
//        sub1.page_index=1;
//        var img: SJGReplaceItemImage=new SJGReplaceItemImage();
//        img.value="http://127.0.0.1:3001/1.jpg";
//        img.pos.w=100;
//        img.pos.h=100;
//        sub1.value=img;
//        root.children.push(sub1);
//
    }
//SJG_MARK_APPEND--------------------


}//SJG_MARK_END-------------------------